package collection;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class ListDemo {
    public static void main(String[] args) {
        List<String> list = new ArrayList<>();
        list.add("one");
        list.add("two");
        list.add("three");
        list.add("four");
        list.add("five");
        list.add("one");
        System.out.println(list);//[one, two, three, four, five, one]

        //E get(int index):获取下标所对应的元素
        String s = list.get(2);
        System.out.println(s);//three
        for (int i = 0; i < list.size(); i++) {
            System.out.print(list.get(i) + " ");//one two three four five one
        }
        System.out.println();
        Iterator<String> it = list.iterator();
        while (it.hasNext()) {
            System.out.print(it.next()+" ");//one two three four five one
        }
        System.out.println();
        for (String str : list) {
            System.out.print(str + " ");//one two three four five one
        }
        System.out.println();
        System.out.println("-------------------------------------");

        //E set(int index,E e):将给定元素设置到指定位置，返回被替换的元素--一般不接受
        //list。set（2，"six"）;//将list中下标为2的元素设置为six，同时将原数据返回old
        String old=list.set(2,"six");
        System.out.println("list:"+list);//list:[one, two, six, four, five, one]
        System.out.println(old);//three

        //E remove(int index):删除指定位置元素，并返回指定位置元素
        //list.remove(2);//删除下标为2的元素，并将被删除元素返回给s
        String str=list.remove(2);
        System.out.println("list:"+list);//list:[one, two, four, five, one]
        System.out.println(str);//six

        /*
        void add(int index,E e):
        将给定元素e添加到index所指定的位置，相当于插入操作
         */
        list.add(3,"seven");
        System.out.println("list:"+list);//list:[one, two, four, seven, five, one]


    }
}
